"""
日志模块
"""

from loguru import logger
import pathlib
import sys
import os

# 终端输出格式
STDOUT_FMT = '<cyan>{time:HH:mm:ss,SSS}</cyan> [<level>{level: <5}</level>] <blue>{module}</blue>:<cyan>{line}</cyan> - <level>{message}</level>'

# 文件输出格式
FILE_FMT = '<light-green>{time:YYYY-MM-DD HH:mm:ss,SSS}</light-green> ' \
                  '[<level>{level: <5}</level>] ' \
                  '<cyan>{process.name}({process.id})</cyan>:' \
                  '<cyan>{thread.name: <10}({thread.id: <5})</cyan> | ' \
                  '<blue>{module}</blue>.<blue>{function}</blue>:' \
                  '<blue>{line}</blue> - <level>{message}</level>'

# 指定日志输出路径
log_path = pathlib.Path(__file__).parent.resolve().joinpath('logs')
if not log_path.is_dir():
    log_path.mkdir()
log_path = log_path.joinpath('web.log').resolve()

# 设置编码
if not os.environ.get('PYTHONIOENCODING'):
    os.environ['PYTHONIOENCODING'] = 'utf-8'

logger.add(sys.stderr, level='INFO', format=STDOUT_FMT, enqueue=True)
logger.add(log_path, level='DEBUG', format=FILE_FMT, enqueue=True, encoding='utf-8')